package restful_apis.entity_matching;

import com.fasterxml.jackson.databind.JsonNode;
import common.helper.json.JavaToJsonString;
import org.springframework.stereotype.Component;
import query_understanding.query_understand.tree_progress.DocumentQueryProgress;
import query_understanding.query_understand.tree_selector.QueryTreeSelector;
import restful_apis.common.MediaTypes;
import semantic_matching.common.schema.MatchedDocumentTree;
import semantic_matching.doc_search.doc_tree.DocumentQueryMatcher;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;

/**
 * Created by julianzliu on 5/5/2017.
 */

@Path("/matching")
@Produces(MediaTypes.JSON_UTF8)
@Component
public class EntitiesMatchingRest {


    private DocumentQueryMatcher entityMatcher;
    private QueryTreeSelector selector;

    public EntitiesMatchingRest(){
        this.entityMatcher = new DocumentQueryMatcher();
        this.selector  = new QueryTreeSelector();
    }

    @GET
    @Path("/_query")
    @Produces(MediaTypes.JSON_UTF8)
    public String  matchEntity(
            @QueryParam("query") String query
    ){

        DocumentQueryProgress progress = this.selector.genDocQueryTree( query );

        MatchedDocumentTree matchedDocumentTree = this.entityMatcher.solveDocumentQueryProgress( progress );

        JsonNode node = matchedDocumentTree.genJsonResult();

        return JavaToJsonString.mapJavaToRestJson( node );
    }

}
